package com.focus.xyz.union.cases.frontend;

import com.focus.xyz.core.common.annotation.Task;
import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.union.action.KeyWordAction;
import com.focus.xyz.core.common.listeners.TC;
import com.focus.xyz.core.utils.ProcessUtil;
import com.focus.xyz.union.data.InsDataBean;
import com.focus.xyz.union.data.InsDataService;
import com.focus.xyz.union.data.SubAcctBean;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

/**
 * Created by chenmeng on 2016/5/24.
 */
public class SubAcctTest extends TC {
    private static transient Logger logger = LoggerFactory.getLogger(SubAcctTest.class);

    private WebDriver driver;
    private KeyWordAction action;


    @Parameters({"nodeUrl","browser","userName","password"})
    @BeforeMethod(alwaysRun = true)
    public void setUp(String nodeUrl,String browser,String userName,String password) throws Exception {
        driver = DriverFactory.getDriver(nodeUrl).getBrowser(browser);
        action = new KeyWordAction(driver);
        super.init(driver);
    }


    @Parameters({"browser"})
    @AfterMethod(alwaysRun = true)
    public void tearDown(String browser) {
        try{
            super.quit();
            driver.quit();
        }catch (Exception e){
            ProcessUtil.closeBrowser(browser);
        }finally {

        }
    }

    @Task(taskName = {"DEL_SUB_ACCT.sql"},paras = {"param0=cimon007_sub007"})
    @Parameters({"userName","password","subAcctIdx","insDataIdx"})
    @Test(description = "新建子账号测试")
    public void ADD_SUB_ACCT(String userName,String password,String subAcctIdx,String insDataIdx) throws Exception{
        action.DelUnionFrontCookie();
        SubAcctBean bean = InsDataService.getSubInfoById(subAcctIdx);
        InsDataBean insData = InsDataService.getInsDataById(insDataIdx);
        action.LoginFront(userName,password);
        action.AddSubAcct(bean,userName,insData.getProdName());
    }


    @Task(taskName = {"DEL_SUB_ORDER_APP_BILL.sql"},paras={"param0=cimon007_sub007"})
    @Parameters({"userName","subAcctIdx","insDataIdx"})
    @Test(description = "子账号投保流程校验")
    public void SUB_ACCT_TOUBAO(String userName,String subAcctIdx,String insDataIdx) throws Exception{
        action.DelUnionFrontCookie();
        SubAcctBean subAcct = InsDataService.getSubInfoById(subAcctIdx);
        InsDataBean insData = InsDataService.getInsDataById(insDataIdx);
        action.LoginFront(userName+"_"+subAcct.getLoginName(),subAcct.getLoginPwd());
        action.OpenToubaoPage(insData.getProdName());
        action.CommonToubao(insData);
        action.assertStringFalse("投保信息不正确");//验证订单是否创建成功
    }


    @Test(description = "子账号支付校验")
    public void SUB_ACCT_PAY() throws Exception{

    }

    @Test(description = "子账号创建订单信息检查")
    public void SUB_ACCT_ORDER_CHECK() throws Exception{

    }

    @Test(description = "子账号交易记录检查")
    public void SUB_TRADE_RECORD_CHECK() throws Exception{

    }

    /*private void delUnionFrontCookie() throws Exception{
        String path = ConfigPropInfo.TEST_DATA_DIR + "cookies/" + ConfigPropInfo.UNION_FRONT_LOGIN_COOKIE_FILE;
        File file = new File(path);
        if(file.exists()){
//            file.delete();
//            FileUtils.deleteQuietly(file);
            FileUtils.forceDelete(file);
            Thread.sleep(2000l);
        }
        CookieUtils.delCookie(driver);//删除浏览器中的cookie
    }*/
}
